#include "DeaconsWidget.h"
#include <iostream>

DeaconsWidget::DeaconsWidget(QWidget *parent)
	: QWidget(parent),
	m_validator(this)
{
	setupUi(this);
	m_validator.setRange(0,100);
	
	installValidator(FamilyGroupBox);
	installValidator(PriesthoodGroupBox);
	installValidator(QuorumGroupBox);
	installValidator(EducationGroupBox);
	installValidator(PhysicalGroupBox);
	installValidator(SpiritualGroupBox);
	installValidator(CitizenshipGroupBox);
	installValidator(ServiceGroupBox);
	installValidator(JournalGroupBox);
		
	quorumentry->setEnabled(false);
	quorumexit->setEnabled(false);
}

DeaconsWidget::~DeaconsWidget()
{
}

void DeaconsWidget::installValidator(QWidget* parent)
{
	foreach(QObject* object, parent->children())
	{
		QLineEdit* line = dynamic_cast<QLineEdit*>(object);
		if (line)
			line->setValidator(&m_validator);
	}
}

void DeaconsWidget::load(YouthPtr youth)
{
	m_youth = youth;
	
    name->setText(m_youth->name());
    birthdate->setDate(m_youth->birthdate());
    phone->setText(m_youth->phoneNumber());
    address->setText(m_youth->address());
    quorumentry->setDate(m_youth->quorumEntryDate());
    lastupdate->setText(m_youth->lastUpdate().toString());   

    loadPriesthood();
    loadFamily();
    loadQuorum();
    loadEducation();
    loadPhysical();
    loadSpiritual();
    loadCitizenship();
    
    loadService();
    loadJournal();
    
    updateCompletions();

}

void DeaconsWidget::updateCompletions()
{
	familycomplete->setText(QString("%1 %").arg(m_youth->deacons().family().completionPercentage()));
	quorumcomplete->setText(QString("%1 %").arg(m_youth->deacons().quorum().completionPercentage()));
	priesthoodcomplete->setText(QString("%1 %").arg(m_youth->deacons().priesthood().completionPercentage()));
	spiritualcomplete->setText(QString("%1 %").arg(m_youth->deacons().spiritual().completionPercentage()));
	physicalcomplete->setText(QString("%1 %").arg(m_youth->deacons().physical().completionPercentage()));
	educationalcomplete->setText(QString("%1 %").arg(m_youth->deacons().education().completionPercentage()));
	citizenshipcomplete->setText(QString("%1 %").arg(m_youth->deacons().citizenship().completionPercentage()));
	servicecomplete->setText(QString("%1 %").arg(m_youth->deacons().service().completionPercentage()));
	journalcomplete->setText(QString("%1 %").arg(m_youth->deacons().journal().completionPercentage()));
	totalcomplete->setText(QString("%1 %").arg(m_youth->deacons().completionPercentage()));
	
    updateTenure();
	
	update();
}

void DeaconsWidget::on_name_textChanged(const QString& string)
{
	m_youth->setName(string);
}

void DeaconsWidget::on_phone_textChanged(const QString& string)
{
	m_youth->setPhoneNumber(string);
}

void DeaconsWidget::on_address_textChanged()
{
    m_youth->setAddress(address->toPlainText());
}

void DeaconsWidget::on_birthdate_dateChanged(const QDate& date)
{
    m_youth->setBirthdate(date);
    
    QDate entry = date.addYears(12);
    m_youth->setQuorumEntryDate(entry);
    quorumentry->setDate(entry);
    
    QDate exit = date.addYears(14);
    quorumexit->setDate(exit);
    updateTenure();
}

void DeaconsWidget::updateTenure()
{
	QDate fourteen = m_youth->birthdate().addYears(14);
	
	float remaining = static_cast<float>(QDate::currentDate().daysTo(fourteen));
	double t = ((730.0 - remaining) / 730.0) * 100.0;
	
	if (t > 100.0) t = 100.0;
	tenure->setText(QString("%1 %").arg(t, 0, 'f', 0));
	
	update();
}

void DeaconsWidget::loadService()
{
	service1->setText(QString("%1").arg(m_youth->deacons().service().req1()));
}

void DeaconsWidget::on_service1_textChanged(const QString& i_percent)
{
    int percent = i_percent.toInt();
    if (percent > 100) percent = 100;
	m_youth->deacons().service().setReq1(percent);
	updateCompletions();
}

void DeaconsWidget::loadJournal()
{
	journal1->setText(QString("%1").arg(m_youth->deacons().journal().req1()));
}

void DeaconsWidget::on_journal1_textChanged(const QString& i_percent)
{
    int percent = i_percent.toInt();
    if (percent > 100) percent = 100;
	m_youth->deacons().journal().setReq1(percent);
	updateCompletions();
}
